package Util::University::University;
use Moose;
use namespace::autoclean;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

sub get_all {
	my ($self) = @_;
	my @university = $self->schema->resultset('University')->all();
	return \@university;
}

sub get_all_univs {
	my ($self) = @_;
	#my @university = $self->schema->resultset('University')->all();
        my @universities = $self->schema->resultset('University')->search(
                        {'type_id' => 1},
                        {});
	return \@universities;
}

sub get_all_offices {
	my ($self) = @_;
	#my @university = $self->schema->resultset('University')->all();
        my @offices = $self->schema->resultset('University')->search(
                        {'type_id' => 2},
                        {});
	return \@offices;
}

sub get_all_houses {
	my ($self) = @_;
	#my @university = $self->schema->resultset('University')->all();
        my @houses = $self->schema->resultset('University')->search(
                        {'type_id' => 3},
                        {});
	return \@houses;
}

sub find {
	my ($self, $univ_id) = @_;
	my $university = $self->schema->resultset('University')->find($univ_id);
	return $university;
}

sub find_most_active_university {
        my ($self, $page, $rows) = @_;
        my @universities;
        my $curr_time = `date --iso-8601=seconds`;
        @universities = $self->schema->resultset('University')->search(
                        {'tuan.status' => 'Y',
                        'tuan.tihuo_time_to' => {'>' => $curr_time},
                        'tuan.tihuo_time_from' => {'<' => $curr_time}
			},
                        {
                                '+select'=> [
                                        {count => 'university_tuan.id', -as => 'tuan_count'}
                                ],
                                order_by => {-desc => [qw/tuan_count/]},
                                group_by => 'me.id',
				rows => $rows,
				page => $page,
                                join => {'university_tuan' =>  'tuan'}
                        }
                );
        return \@universities;
}

__PACKAGE__->meta->make_immutable;

1;
